﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using InterfacesRegconition;
using DTO;

namespace MyMeanFeatureVector
{
    public partial class MeanFeatureVectorPlugin : UserControl, IFeatureVectorCalculator
    {
        public MeanFeatureVectorPlugin()
        {
            InitializeComponent();
        }


        private IRegconitionHost pluginHost = null;
        public IRegconitionHost PluginHost
        {
            get
            {
                return pluginHost;
            }
            set
            {
                this.pluginHost = value;
            }
        }

        private string pluginName = "Mean Feature Vector Calculator Plugin";
        public string PluginName
        {
            get
            {
                return this.pluginName;
            }
        }

        private string pluginDescription = "Calculate the feature vector in a list of vectors by find the mean elements in each dimension.";

        public string PluginDescription
        {
            get { return pluginDescription; }
            set { pluginDescription = value; }
        }

        public UserControl PluginUserControl
        {
            get
            {
                return this;
            }
        }

        public void Initialize()
        {
        }

        public void PluginDispose()
        {

        }

        #region Interfaces Implementation
        public Vector CalcFeatureVector(List<Vector> vectorList)
        {
            Vector sumVector = new Vector();
            sumVector.Value = vectorList[0].Value;

            try
            {
                for (int i = 1; i < vectorList.Count; i++)
                {
                    for (int j = 0; j < vectorList[i].Value.Count; j++)
                    {
                        double sum = sumVector.Value[j] + vectorList[i].Value[j];
                        sumVector.Value[j] = sum;
                    }
                }
            }
            catch (IndexOutOfRangeException ex)
            {
                throw ex;
            }

            Vector featureVector = new Vector();
            featureVector.Value = new List<double>(new double[sumVector.Value.Count]);
            
            double dimension = Convert.ToDouble(sumVector.Value.Count);
            try
            {
                for (int i = 0; i < sumVector.Value.Count; i++)
                {
                    featureVector.Value[i] = sumVector.Value[i] / dimension;
                }
            }
            catch (DivideByZeroException ex)
            {
                throw ex;
            }

            return featureVector;
        }
        #endregion
    }
}
